package com.servicesDirectory.action.login;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.opensymphony.xwork2.ActionSupport;
import com.servicesDirectory.dao.login.LoginDAOImpl;

public class ForgotPass extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String account;    
    private String email;
    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
    public String execute() {
		LoginDAOImpl lgDAO = new LoginDAOImpl();
    	boolean result = lgDAO.checkMail(account, email);
    	String mResult;
    	if(result)
    	{
    		addActionError("Your password has been sent to you email");
    		mResult = "success";
    	}
    	else {
    		mResult= "error";
    		addActionError("*Username and Email do not match");
    	}
    	
    	return mResult;
    }
 
    public void validate(){
    	String regex = "^([\\w-\\.])+@([\\w])+\\.(\\w){2,6}(\\.([\\w]){2,4})*$";
    	Pattern p = Pattern.compile(regex);
    	Matcher matcher = p.matcher(this.email);
    	if(this.account.length() == 0 ){
    		addActionError("*Username is required");
    	}else if(this.account.length() < 4){
    		addActionError("*Username is very short");
    	}else if(this.account.length() > 15){
    		addActionError("*Username is too long");
    	}
    	
    	if(this.email.length() == 0 ){
    		addActionError("*Email is required");
    	}else if(!matcher.find()){
    		addActionError("*Email is not invalid");
    	}      	
    }
    public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}
}
